#include"comm.hpp"

void readMesg(int fd)
{
    // 文件读取
    char buf[SIZE];
    while(true)
    {
        int ret = read(fd, buf, sizeof(buf) - 1);
        if(ret > 0)
        {
            buf[ret] = 0;
            cout << "childProcess[" << getpid() << "] get mesg::" << buf << endl;
        }
        else if(ret == 0)
        {
            Log("read over", Debug);
            exit(3);
        }
        else
        {
            Log("read error", Debug);
            exit(4);
        }
    }
}

int main()
{
    cout << umask(0) << endl;
    // 创建管道文件
    if(mkfifo(str, 0666) < 0)
    {
        // 创建文件失败
        perror("mkfifo");
        exit(1);
    }
    Log("server create fifo", Debug);

    // 正常文件操作
    int fd = open(str, O_RDONLY);
    if(fd == -1)
    {
        // 打开文件出错，打印日志
        Log("open fail", Error);
        exit(2);
    }

    int processNum = 3;
    for(int i = 0; i < processNum; ++i)
    {
        pid_t id = fork();
        if(id == 0)
        {
            Log("child process readMesg", Debug);
            readMesg(fd);
        }
    }

    for(int i = 0; i < processNum; ++i)
    {
        waitpid(-1, nullptr, 0);
    }
    

    // 关闭文件描述符
    close(fd);

    return 0;
}